package modules.majiang.viewManager
{
	/**
	 *
	 * @author xumin.xu 2013-2-4
	 */

	import com.netease.protobuf.UInt64;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	
	import modules.ViewManager;
	import modules.majiang.MajiangEvent;
	import modules.majiang.data.CountDownState;
	import modules.majiang.data.TableDataModel;
	import modules.majiang.data.TablesDataManager;

	public class CountdownViewManager extends ViewManager
	{
		private var tableId:UInt64;

		private var preCountDownStateList:Array = [];

		/** 总帧数 */
		private var totalFrame:int = 60;

		public function CountdownViewManager(tableId:UInt64,tableView:MovieClip)
		{
			super();
			this.tableId = tableId;
			
			this.mainUI = tableView;

			tableView.countDown_1.countDown.visible = false;
			tableView.countDown_2.countDown.visible = false;
			tableView.countDown_3.countDown.visible = false;
			tableView.countDown_4.countDown.visible = false;
			tableView.countDown_5.countDown.visible = false;
			tableView.countDown_6.countDown.visible = false;

			tableView.countDown_1.countDown.maskMc.gotoAndStop(1);
			tableView.countDown_2.countDown.maskMc.gotoAndStop(1);
			tableView.countDown_3.countDown.maskMc.gotoAndStop(1);
			tableView.countDown_4.countDown.maskMc.gotoAndStop(1);
			tableView.countDown_5.countDown.maskMc.gotoAndStop(1);
			tableView.countDown_6.countDown.maskMc.gotoAndStop(1);

			tableView.addEventListener(Event.ADDED_TO_STAGE, onAddToStage);
			tableView.addEventListener(Event.REMOVED_FROM_STAGE, removeFromStage);
		}

		public function get tableDataModel():TableDataModel
		{
			return TablesDataManager.tableDataModeDic[tableId.toString()];
		}

		private function onAddToStage(event:Event):void
		{
			//更新麻将桌子界面中显示
			dispatcher.addEventListener(MajiangEvent.UPDATE_MAJIANG_TABLE_VIEW, function(event:MajiangEvent):void
			{
				if (tableId.toString() != event.data.tableId.toString())
					return;
				updateView();
			});

		}

		private function removeFromStage(event:Event):void
		{

		}

		private function updateView():void
		{
			for (var i:int = 1; i <= 6; i++)
			{
				var chairId:int = tableDataModel.getChairIdByPosition(i);
				var countDownState:CountDownState = tableDataModel.countDownStates[chairId];
				var preCountDownState:CountDownState = preCountDownStateList[chairId];
				if (preCountDownState != null)
				{
					preCountDownState.endFunc = null;
					preCountDownState.stepFunc = null;
					preCountDownState.countDownView = null;
				}
				preCountDownState = countDownState;
				if (preCountDownState != null)
				{
					preCountDownState.stepFunc = stepFunc;
					preCountDownState.endFunc = endFunc;
					preCountDownState.countDownView = mainUI["countDown_" + i];
				}
			}
			preCountDownStateList = tableDataModel.countDownStates;
		}

		private function stepFunc(countDownView:MovieClip, value:Number):void
		{
			var nextFrame:uint = totalFrame * value;
			countDownView.countDown.maskMc.gotoAndStop(nextFrame);
			countDownView.countDown.visible = true;
		}

		private function endFunc(countDownView:MovieClip):void
		{
			countDownView.countDown.visible = false;
		}
	}
}
